import { addDialog } from '@/components/ReDialog'
import { ref, onMounted, h } from "vue";
import { message } from "@/utils/message";
import dxfForm from '../dxfForm.vue'
import agvForm from '../agvForm.vue'
import { GetAllAgv, Comfirmloc, Reloc } from '@/utils/api/dispatch/agv'
import { CreateMove } from '@/utils/api/dispatch/job'
import { GetAllStation } from '@/utils/api/BasicManagement/station'
import { ElMessageBox } from 'element-plus'
// import { $t } from "@/plugins/i18n";

export function useDept() {
    const formRef = ref();

    const agvOptions = ref([])
    const stationSelect = ref([])

    async function agvMove(row?: any) {
        // agv
        let { Content } = await GetAllAgv({ state: 1, model: 0 })
        agvOptions.value = Content

        // 站点列表
        let { Content: station } = await GetAllStation()
        stationSelect.value = station

        addDialog({
            title: `移动任务`,
            props: {
                formInline: {
                    agvId: null,
                    toStationId: row?.stationid ?? null
                },
                agvOptions: agvOptions.value || [],
                stationSelect: stationSelect.value || [],
            },
            width: "40%",
            draggable: true,
            fullscreenIcon: true,
            closeOnClickModal: false,
            contentRenderer: () => h(dxfForm, { ref: formRef } as any),
            beforeSure: (done, { options }) => {
                const FormRef = formRef.value.getRef();
                const curData = options.props.formInline;
                function chores() {
                    message(`移动成功`, {
                        type: "success"
                    });
                    done(); // 关闭弹框
                }
                FormRef.validate(valid => {
                    if (valid) {
                        console.log("curData", curData);
                        // 表单规则校验通过
                        CreateMove(curData).then(res => {
                            if (res.Code === 0) chores()
                        })
                    }
                });
            }
        })
    }

    // 定位
    function agvPosition(data, type) {
        if (!type) {
            // 确定定位
            ElMessageBox.confirm(
                `是否确认定位${data.agvname}车辆?`,
                '提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true,
                }
            ).then(() => {
                Comfirmloc({ agvID: data.agvid }).then(res => {
                    if (res.Code === 0) {
                        message('确认成功');
                    }
                })
            }).catch(() => { })
        } else {
            // 手动定位
            addDialog({
                title: `手动定位`,
                props: {
                    formInline: {
                        x: null,
                        y: null,
                        angle: null,
                        agvID: data?.agvid || null
                    }
                },
                width: "40%",
                draggable: true,
                fullscreenIcon: true,
                closeOnClickModal: false,
                contentRenderer: () => h(agvForm, { ref: formRef } as any),
                beforeSure: (done, { options }) => {
                    const FormRef = formRef.value.getRef();
                    const curData = options.props.formInline;
                    function chores() {
                        message(`定位成功`, {
                            type: "success"
                        });
                        done(); // 关闭弹框
                    }
                    FormRef.validate(valid => {
                        if (valid) {
                            console.log("curData", curData);
                            // 表单规则校验通过
                            Reloc(curData).then(res => {
                                if (res.Code === 0) chores()
                            })
                        }
                    });
                }
            })
        }
    }

    return {
        agvMove,
        agvPosition
    }
}

